package com.itheima.mall.web.ums;


import com.itheima.mall.pojo.ums.Admin;
import com.itheima.mall.pojo.ums.UmsRole;
import com.itheima.mall.service.ums.AdminService;
import com.itheima.mall.vo.AdminInfoVo;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 登录并返回token
     * @param map
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Map map){
        Map token = adminService.login(map);
        return R.succeed(token,"登录成功");
    }

    /**
     * 登出
     * @return
     */
    @PostMapping("logout")
    public R logout(){
        adminService.logout();
        return R.succeed(null,"退出成功");
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("info")
    public R info(){
        AdminInfoVo adminInfoVo = adminService.info();
        return R.succeed(adminInfoVo,"操作成功");
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delAdmin(@PathVariable("id") Long id){
        adminService.delAdmin(id);
        return R.succeed(null,"删除成功");
    }


    /**
     * 根据用户名或姓名分页获取用户列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public R list(String keyword,Integer pageNum,Integer pageSize){
        PageResult adminList = adminService.list(keyword,pageNum,pageSize);
        return R.succeed(adminList,null);
    }

    /**
     * 注册
     * @param admin
     * @return
     */
    @PostMapping("register")
    public R register(@RequestBody Admin admin){
        Admin res = adminService.register(admin);
        return R.succeed(res,"注册成功");
    }

    /**
     * 给用户分配角色
     * @param
     * @return
     */
    @PostMapping("role/update")
    public R roleUpdate(Long[] roleIds,@RequestParam("adminId") Long adminId){
        adminService.roleUpdate(roleIds,adminId);
        return R.succeed(null,"更新成功");
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("role/{adminId}")
    public R getUserRole(@PathVariable("adminId") Long id){
        List<UmsRole> umsRoleList = adminService.getUserRole(id);
        return R.succeed(umsRoleList,null);
    }

    /**
     * 根据id查询指定用户信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getUserInfoById(@PathVariable("id") Long id){
        Admin admin = adminService.getUserInfoById(id);
        return R.succeed(admin,null);
    }

    /**
     * 根据id更新用户信息
     * @param id
     * @param admin
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateUserInfoById(@PathVariable("id") Long id,@RequestBody Admin admin){
        admin.setId(id);
        adminService.updateUserInfoById(admin);
        return R.succeed(null,"更新成功");
    }

    /**
     * 修改指定用户密码
     * @param map
     * @return
     */
    @PostMapping("updatePassword")
    public R updatePassword(@RequestBody Map map){
        adminService.updatePassword(map);
        return R.succeed(null,"修改成功");
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("updateStatus/{id}")
    public R updateStatus(@PathVariable("id") Long id,@RequestParam("status") Integer status){
        adminService.updateStatus(id,status);
        return R.succeed(null,"修改成功");
    }
}
